import cv2
import time
import oss2

camera = cv2.VideoCapture(0)

# 配置阿里云OSS的AccessKeyId、AccessKeySecret和Endpoint
access_key_id = 'LTAI5t9E4qg8Y2KYp87rDpUZ'
access_key_secret = 'HQjr3Bih4uE5iaE5fDF2PQ8MnEjg9M'
endpoint = 'oss-cn-beijing.aliyuncs.com'
bucket_name = 'ekko-pi'

# 创建OSS存储空间（Bucket）对象
auth = oss2.Auth(access_key_id, access_key_secret)
bucket = oss2.Bucket(auth, endpoint, bucket_name)

while True:

    ret, frame = camera.read()
    time.sleep(0.2)
    # 保存图片
    cv2.imwrite("face.jpg", frame)
    time.sleep(0.2)

    t = time.time()
    hour = time.localtime(t).tm_hour
    minute = time.localtime(t).tm_min
    second = time.localtime(t).tm_sec
    # second变为后1s
    second = second + 1
    if second == 60:
        second = 0
        minute = minute + 1
        if minute == 60:
            minute = 0
            hour = hour + 1
            if hour == 24:
                hour = 0
                



    # 上传图片到OSS
    flag = str(hour) + str(minute) + str(second)
    object_key = flag + 'face.jpg'  # 设置图片在OSS中的对象名（Key）
    local_file = 'face.jpg'  # 设置本地图片文件路径
    bucket.put_object_from_file(object_key, local_file)

    # 打印上传后的图片URL

    image_url = 'https://ekko-pi.oss-cn-beijing.aliyuncs.com/' + object_key
    print("Uploaded image URL:", image_url)
    time.sleep(0.4)
